#include "LastModify.h"
#include <Poco/Path.h>
#include <Poco/Util/LayeredConfiguration.h>
#include <Poco/File.h>
#include <Poco/Util/AbstractConfiguration.h>
#include <Poco/Util/PropertyFileConfiguration.h>
#include <Poco/RecursiveDirectoryIterator.h>
#include <fstream>

#include "global.h"
using Poco::Util::AbstractConfiguration;
using Poco::Util::PropertyFileConfiguration;
using Poco::SiblingsFirstRecursiveDirectoryIterator;
using Poco::Path;
using Poco::File;
using std::map;

LastModify::LastModify(Poco::Util::LayeredConfiguration& config):_config(config)
{
	
}



void LastModify::createSrcLM(std::map<std::string, time_t>& map)
{
	createLM(SRC_DIR_KEY, map);
}

void LastModify::createSrcsLM(std::map<std::string, time_t>& map)
{
	createLM(CSRC_DIR_KEY, map);
}


LastModify::~LastModify()
{
}

void LastModify::createLM(const std::string &dirname, std::map<std::string, time_t>& map)
{
	Path path = Path().absolute().pushDirectory(_config.getString(dirname));
	SiblingsFirstRecursiveDirectoryIterator end;
	std::string dirstr = path.toString();
	SiblingsFirstRecursiveDirectoryIterator rdit(dirstr);

	while (rdit != end)
	{
		if (!rdit->isFile())
		{
			++rdit;
			continue;
		}
		Path inPath(rdit->path());
		std::string inpathstr = rdit->path();
		std::string outpathstr = inpathstr.substr(dirstr.length());
		time_t epoch = rdit->getLastModified().epochTime();
		map[outpathstr] = epoch;
		++rdit;
	}
}
